﻿//获取当前登录用户信息
function currentUser(f) {
    $.post("../User/AjaxGetCurrentUser", {}, function (res) {
        f(res);
    });
}

$.getJSON("../Home/GetNotReadMessage", { now: new Date() }, function (result) {
    if ($("#spanMsg")) {
        $("#spanMsg").remove();
    }
    if (parseInt(result) > 0) {
        $("<span>", { html: result, "class": "notReadMsg",id:"spanMsg"}).appendTo("#aUserName");
    }
});

//点击需要访问权限的超链接或者按钮激发的事件
function EnteWayClick() {
    var rolerStr = $(this).attr("data-roler");//点击的超链接上的角色信息
    var specialLink = $(this).attr("data-link");//指定跳转链接
    currentUser(function (r) {
        if (r == "null") {
            $("#loginModel").modal("show");
            $("#returnUrl").val("");
            if (specialLink) {
                $("#returnUrl").val(specialLink); //制定跳转链接
            }
            //alert($("#returnUrl").val());
        }
        else {
            var user = eval("(" + r + ")");
            //var roler = user.Roler;
            var rolerName = user.RolerName;
            var url = user.Url;//默认跳转地址
            if (rolerStr.indexOf(rolerName) == -1) {
                alert("权限不够,当前登录角色:" + rolerName);
            } else {
                //alert(specialLink);
                if (specialLink) {
                    location.href = specialLink;//指定跳转地址
                } else
                    if (url != undefined || url != "") {
                        location.href = url;//有默认地址则跳转默认地址
                    }
                    else {
                        location.href = "./Home";//否则首页
                    }
            }
        }
    });
}

$(function () {
    $("#btnLogin").live("click", btnLoginClick);

    document.onkeydown = function (e) {
        var ev = document.all ? window.event : e;
        if (ev.keyCode == 13) { //回车键的键值为13 
            var isShow = $("#loginModel").css("display");
            if (isShow == "block") {
                $("#btnLogin").click();
            }
        }
    };

    //登录
    function btnLoginClick() {
        var $btn = $(this);
        var oldStr = $btn.text();
        var url = $("#returnUrl").val();
        var userName = $("#loginModel #LoginName").val();
        var pwd = $("#loginModel #Password").val();

        if (userName == "") {
            alert("请输入用户名");
            $("#loginModel #LoginName").focus();
            return;
        }

        if (pwd == "") {
            alert("请输入密码");
            $("#loginModel #Password").focus();
            return;
        }
        $.ajax({
            url: "../User/Login",
            data: { userName: userName, passWord: pwd },
            dataType: "json",
            beforeSend: function () {
                $btn.text("登录中...");
            },
            type: "post",
            success: function (res) {
                if (res.Result.IsSuccess) {
                    //alert(url);
                    if (url != "" && url != undefined) {
                        //location.href = url;
                        location.reload();
                    } else {
                        //alert(res.Result.Message);
                        location.href = res.Result.Message;
                    }
                    $("#loginModel").modal("hide");
                }
                else {
                    alert(res.Result.ErrMessage);
                }
            },
            async: true,
            complete: function () {
                $btn.text(oldStr);
            }
        });
        //
    };

    var title = this.title;
    var liId = "";
    var redClass = "";
    switch (title)
    {
        case "快车特色":
            liId = "nav2";
            redClass = "readLiZ";
            break;
        case "联系我们":
            liId = "nav2";
            redClass = "readLiZ";
        case "关于出国":
            liId = "nav3";
            redClass = "readLiZ";
            break;
        case "项目介绍":
            liId = "nav4";
            redClass = "redLi";
            break;
        case "出国流程":
            liId = "nav5";
            redClass = "redLi";
            break;
        case "网上咨询(Q&A)":
            liId = "nav6";
            redClass = "redLi";
            break;
    }
    if (liId) {
        //alert(liId+"--"+redClass)
        $("#" + liId).removeClass("garyLiZ").removeClass("garyLi").addClass(redClass);
    }
});